<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Spring Authorization Server sample</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css"
          integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
    <link rel="stylesheet" href="/assets/css/signin.css" th:href="@{/assets/css/signin.css}"/>
</head>
<body>
<div class="container">
    <form class="form-signin w-100 m-auto" method="post" th:action="@{/user/login/username}">
        <!-- 显示错误信息 -->
        <div th:if="${session.loginError}" class="alert alert-danger" role="alert">
            <p th:text="${session.loginError}">An error occurred.</p>
        </div>
        <h1 class="h3 mb-3 fw-normal">Please sign in</h1>
        <div class="form-floating">
            <input type="text" id="username" name="username" class="form-control" required autofocus>
            <label for="username">Username</label>
        </div>
        <div class="form-floating">
            <input type="password" id="password" name="password" class="form-control" required>
            <label for="password">Password</label>
        </div>
        <div>
            <label for="captcha">Captcha:</label>
            <input type="text" id="captcha" name="captcha" required>
            <img id="captchaImg" th:src="'http://localhost:7070/auth/captchab/' + ${uuid}" alt="Captcha" onclick="refreshCaptcha();"><br>
        </div>

        <!-- 存储 redirect 参数的值 -->
        <input type="hidden" name="redirect" th:value="${redirect}">
        <input type="hidden" name="uuid" th:value="${uuid}">
        <div>
            <button class="w-100 btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
            <a class="w-100 btn btn-light btn-block bg-white"
               role="link"
               style="margin-top: 10px"
               href="/auth/oauth2/authorization/gitee">
                <img src="/assets/img/google.png" th:src="@{/assets/img/github.png}" width="20"
                     style="margin-right: 5px;" alt="Sign in with Google">
                Sign in with Gitee
            </a>
            <!--            <a class="w-100 btn btn-light btn-block bg-white" href="/oauth2/authorization/github-idp" role="link"-->
            <!--               style="margin-top: 10px">-->
            <!--                <img src="/assets/img/github.png" th:src="@{/assets/img/github.png}" width="24"-->
            <!--                     style="margin-right: 5px;" alt="Sign in with Github">-->
            <!--                Sign in with Github123-->
            <!--            </a>-->
        </div>
    </form>

</div>
</body>
<script>
    function refreshCaptcha() {
        // 发送请求获取新的 uuid
        fetch('/auth/getNewUuid')
            .then(response => response.text())
            .then(uuid => {
                // 更新图片的 src 属性
                const captchaImg = document.getElementById('captchaImg');
                captchaImg.src = 'http://localhost:7070/auth/captchab/' + uuid + '?' + Math.random();
            })
            .catch(error => console.error('获取新 uuid 失败:', error));
    }
</script>
</html>
